package com.eascs.accessory.controller;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import com.alibaba.dubbo.config.annotation.Reference;
import com.alibaba.fastjson.JSON;
import com.eascs.accessory.entity.Accessory;
import com.eascs.accessory.service.AccessoryService;
import com.eascs.admin.common.controller.BaseMasterViewController;
import com.eascs.app.dao.PageResult;
import com.eascs.datalist.entity.O2oDataListEntity;
import com.eascs.site.page.PaginationInfo;
import com.eascs.site.view.ViewContext;
import com.eascs.site.view.button.Button;
import com.eascs.site.view.button.ButtonHelper;
import com.eascs.tag.MenuTag;
import com.eascs.web.sso.User;
import com.eascs.web.sso.UserHolder;
import com.eascs.web.url.UrlHelper;
import com.eascs.workflow.common.entity.sys.ActOrgEntity;
import com.eascs.workflow.common.service.sys.SysOrgService;
import com.eascs.workflow.common.vo.ModelResult;

@Controller
@RequestMapping(value = "/accessory")
public class AccessoryController extends BaseMasterViewController{
	@Inject
	AccessoryService accessoryService;
	
	@Reference
	SysOrgService service;
	
	public void preHandle() {
		super.preHandle();
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "operationalGuidelines");
	}
	@RequestMapping("/fileUploadPage")
	public String upload(String accessoryId) {
		ModelResult<Accessory> model = accessoryService.findEnTityById(accessoryId);
		Accessory accessory = null;
		if (model.isSuccess()) {
			accessory = model.getModel();
		}
		ViewContext.put("datalistInfo", getDatalistInfo(accessoryId));
		ViewContext.put("accessory", accessory);
		return "/accessory/upload";
	}

	/**
	 * 附件列表
	 * 
	 * @param loanapplyNo
	 * @return
	 */
	public PaginationInfo getDatalistInfo(String accessoryId) {
		//获取登录人信息
		User user = UserHolder.getUser();
		ModelResult<PageResult<Object[]>> model = new ModelResult<PageResult<Object[]>>();
		//根据登录角色数据权限判断是否是总部
		if("99=99and".equals(user.getDataPermissionStr().replace(" ", ""))){
			model= accessoryService.findO2oDataListEntityById(accessoryId);
		}else{
			ModelResult<List<ActOrgEntity>> proModel = service.getProvinceWithUser(user.getNo());
			List<ActOrgEntity> listPro = proModel.getModel();
			model= accessoryService.findO2oDataListEntityByIdNotZB(accessoryId, listPro);
		}
		PageResult<Object[]> pageResult = null;
		if (model.isSuccess()) {
			pageResult = model.getModel();
		}
		String titles[] = {"文件名", "提交人", "提交时间"};
		Button download = ButtonHelper.createOpenButton("预览", "/file/download?datalistno=");
		Button delFile = ButtonHelper.createAjaxConfirmButton("删除", "/accessory/delFile?datalistno=");
		Button updateFile = ButtonHelper.createAjaxButton("修改", "/accessory/accessory/updateFile?datalistno=");
		Button addAccProvine = ButtonHelper.createAjaxButton("查看权限设置", "/accessory/toAddAccProvine?associatedId=");
		delFile.setPermissionCode("delFile");
		updateFile.setPermissionCode("updateFile");
		addAccProvine.setPermissionCode("addAccProvine");
		Button[] findButton = new Button[] { download, delFile, updateFile, addAccProvine };
		PaginationInfo datalistInfo = new PaginationInfo(pageResult, titles, "/loanUser/loanudpate");
		datalistInfo.setMoreButtons(findButton);
		datalistInfo.setBuildPagingNavigation(false);
		datalistInfo.setFirstRenderColumnIndex(1);
		datalistInfo.setBuildRowNumber(true);
		return datalistInfo;
	}

	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list() {
		//systemSettings
		ViewContext.put(MenuTag.CURRENT_MENU_CODE, "caozuozhiyin");
		ModelResult<List<Accessory>> modelResult = accessoryService.selectAccessoryList();
		List<Accessory> list = modelResult.getModel();
		ViewContext.put("reso", JSON.toJSONString(list));
		ViewContext.put("urls", UrlHelper.build("/accessory/othfile/GUIDELINE/"));
		return "/accessory/resourceList";
	}

	/**
	 * 
	 * @Title: interview
	 * @Description:面签指引列表
	 * @author user
	 * @return
	 * @return String
	 * @throws @date
	 * 2016年8月9日 下午3:56:00/accessory/help/interview
	 */
	@RequestMapping(value = "/help/interview", method = RequestMethod.GET)
	public String interview(String resourceCode,@RequestParam(value="master",required=false) String master) {
		if(master == null){
			ViewContext.put("masterType","master-loan");
		}else{
			ViewContext.put("masterType",master);
		}

		if(("customerManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","面签指引");
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "interview");
		}else if(("loanManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","业务指引");
		}else if(("approvalManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","有问必答");
		}else if(("operationalGuidelines").equals(resourceCode)){
			ViewContext.put("accessoryTitle","业务发文");
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "business");
		}else {
			ViewContext.put(MenuTag.CURRENT_MENU_CODE, "user manuals");
		}
		
		//指引分类
		Accessory accessory=accessoryService.findAccessoryByResourceCode(resourceCode);
		List<O2oDataListEntity> accessoryattachs= accessoryService.findFileByAccessroyId(accessory.getId());
		accessory.setSubDataListEntity(accessoryattachs);
		ViewContext.put("accessory", accessory);
		//一级标题
		ModelResult<List<Accessory>> levelOneResult=accessoryService.findNodeByParentId(accessory.getId());
		List<Accessory> levelOneList=levelOneResult.getModel();
		for(Accessory levelOne : levelOneList){
			//一级附件
			List<O2oDataListEntity> levelOneattachs= accessoryService.findFileByAccessroyId(levelOne.getId());
			levelOne.setSubDataListEntity(levelOneattachs);
		}
		
		
		for (Accessory levelOne : levelOneList) {
			//二级标题
			ModelResult<List<Accessory>> levelTwoResult=accessoryService.findNodeByParentId(levelOne.getId());
			List<Accessory> levelTwoList=levelTwoResult.getModel();
			
			levelOne.setSubResources(levelTwoList);
			
			for(Accessory levelTwo : levelTwoList){
				//二级附件
				List<O2oDataListEntity> levelTwoattachs= accessoryService.findFileByAccessroyId(levelTwo.getId());
				levelTwo.setSubDataListEntity(levelTwoattachs);
			}
		}
		ViewContext.put("accessoryViews", levelOneList);
		
		return "/help/interview";
	}
	/**
	 * 
	 * @Title: interview
	 * @Description:空白合同跳转到业务指引下载合同
	 * @author user
	 * @return
	 * @return String
	 * @throws @date
	 * 2016年8月9日 下午3:56:00/accessory/help/interview
	 */
	@RequestMapping(value = "/help/downContract", method = RequestMethod.GET)
	public String toContract(String resourceCode) {
		if(("customerManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","面签指引");
		}else if(("loanManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","业务指引");
		}else if(("approvalManage").equals(resourceCode)){
			ViewContext.put("accessoryTitle","有问必答");
		}else if(("operationalGuidelines").equals(resourceCode)){
			ViewContext.put("accessoryTitle","业务发文");
		}
		Accessory accessory=accessoryService.findAccessoryByResourceCode(resourceCode);
		ViewContext.put("accessory", accessory);
		ModelResult<List<Accessory>> subres=accessoryService.findNodeByParentId(accessory.getId());
		List<Accessory> views=subres.getModel();
		for (Accessory view : views) {
			ModelResult<List<Accessory>> subView=accessoryService.findNodeByParentId(view.getId());
			view.setSubResources(subView.getModel());
		}
		ViewContext.put("accessoryViews", views);
		return "/help/downContract";
	}	
}
